package lanqiao.api;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 大数运算
 *
 * BigInteger的加减乘除
 */

public class BigIntegerDemo {
    public static void main(String[] args) {
        // 2021年蓝桥杯八次幂求和
//        BigInteger begin;
//        //初始值为设置为0
//        BigInteger sum = BigInteger.ZERO;
//        Scanner scanner = new Scanner(System.in);
//        int n = scanner.nextInt();
//        for (int i = 1; i <= n; i++) {
//            begin = BigInteger.valueOf(i).pow(8);
//            sum = sum.add(begin);
//        }
//        String mod = "123456789";
//        System.out.println(sum.mod(new BigInteger(mod)));

        System.out.println("==========================");
        f1();
    }


    /*
     * public BigInteger add(BigInteger val):加
     * public BigInteger subtract(BigInteger val):减
     * public BigInteger multiply(BigInteger val):乘
     * public BigInteger divide(BigInteger val):除
     * public BigInteger[] divideAndRemainder(BingInteger val):返回商和余数的数组
     */
    public static void f1(){
        //加减乘除
        System.out.println(new BigInteger("1").add(new BigInteger("2")));
        System.out.println(new BigInteger("1").subtract(new BigInteger("2")));
        System.out.println(new BigInteger("12").multiply(new BigInteger("2")));
        System.out.println(new BigInteger("12").divide(new BigInteger("2")));

        BigInteger[] nums = new BigInteger("12").divideAndRemainder(new BigInteger("2"));
        System.out.println(Arrays.toString(nums));

        System.out.println("================================================================");
        BigInteger a = BigInteger.ONE;
        BigInteger b = BigInteger.valueOf(4);
        //判断大小 a>b返回1，小于返回-1，等于返回0
        System.out.println(a.compareTo(b));
        //判断是否相等，返回boolean
        System.out.println("equals:"+a.equals(b));
        //二进制的翻转，把数字的二进制对应的位数翻转，例如4的二进制是100，将第二为的0翻转为1，变成 110----> 6
        System.out.println("flipBit："+b.flipBit(1));
        //最大公约数,返回两个数的最大公约数
        System.out.println(new BigInteger("15").gcd(new BigInteger("30")));
        // intValue、floatValue等等，将BigInteger转换为对应类型
        // max,min,返回两个数中最大，最小的
        System.out.println("max:"+b.max(a));
        // mod 模运算 返回的是余数
        System.out.println("mod:"+b.mod(new BigInteger("3")));
        // 其值是 (this-1 mod m).
        System.out.println("modInverse:"+b.modInverse(new BigInteger("3")));

        //幂运算
        System.out.println(b.toString()+"**"+"3 = "+b.pow(3));
        //modPow
        System.out.println(b.modPow(a,a));
        //negate()  取反
        System.out.println(b.negate());
        //逻辑运算 not or

        // 4%3   模运算和取余运算的区别在于符号   a%b 结果的正负只与a有关
        System.out.println("%:"+b.negate().remainder(new BigInteger("3")));
        System.out.println("mod:"+b.negate().mod(new BigInteger("3")));

        //二进制运算暂时不用

    }
}
